package cn.hs.lyxf.result;

import cn.hs.lyxf.exception.GlobalCustomizeException;
import cn.hs.lyxf.exception.GlobalErrorCode;
import lombok.Data;

//返回JSON结果
@Data
//建造者模式
//@Builder
public class JsonResult {

    private boolean success = true;

    private String message = "成功";

    //错误码，用来描述错误类型 ，1000 表示没有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    /**
     * 创建当前实例
     **/
    public static JsonResult success() {
        return new JsonResult();
    }

    /**
     * 创建当前实例
     **/
    public static JsonResult success(Object obj) {
        JsonResult instance = new JsonResult();
        instance.setData(obj);
        return instance;
    }

    public static JsonResult success(Object obj, String code) {
        JsonResult instance = new JsonResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }

    /**
     * 创建当前实例
     **/

    public static JsonResult error(String message, String code) {
        JsonResult instance = new JsonResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }

    public static JsonResult error(GlobalErrorCode globalErrorCode) {
        JsonResult instance = new JsonResult();
        instance.setMessage(globalErrorCode.getMessage());
        instance.setSuccess(false);
        instance.setCode(globalErrorCode.getCode());
        return instance;
    }

    public static JsonResult error(GlobalCustomizeException e) {
        JsonResult instance = new JsonResult();
        // 获取到祖宗类返回的detailMessage
        instance.setMessage(e.getMessage());
        instance.setSuccess(false);
        instance.setCode(e.getCode());
        return instance;
    }

    public static JsonResult error() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /**
     * 创建当前实例
     **/
    public static JsonResult error(String message) {
        return error(message, null);
    }

}